
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:yp/pages/order_settlement/widgets/style.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';


class CachedImage extends StatelessWidget {
  final String path;
  final List<BoxShadow> shadows;

  /// 圆角 默认为 4
  final double borderRadius;
  final double height;
  final double width;
  final BorderRadius radius;
  final BoxFit scale;
  final BoxBorder border;
  final Color bgColor;
  final Alignment alignment;

  /// 边距
  final EdgeInsetsGeometry margin;

  final EdgeInsetsGeometry padding;

  CachedImage(
      {Key key,
      @required this.path,
      this.borderRadius = 4,
      this.radius,
      this.scale,
      this.height = 50,
      this.width = 50,
      this.margin = const EdgeInsets.all(0),
      this.padding = const EdgeInsets.all(0),
      this.shadows,
      this.border,
      this.alignment,
      this.bgColor})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if ((path == null || path.isEmpty)) {
      return _buildEmpty();
    } else {
      return CachedNetworkImage(
        imageUrl: path,
        imageBuilder: (context, imageProvider) => Container(
          height: height,
          width: width,
          margin: margin,
          padding: padding,
          decoration: BoxDecoration(
              color: bgColor != null ? bgColor : Color(0xFFF0F0F0),
              borderRadius:
                  radius != null ? radius : BorderRadius.circular(borderRadius),
              image: DecorationImage(
                  image: imageProvider,
                  fit: scale == null ? BoxFit.fill : scale,
                  alignment: alignment != null ? alignment : Alignment.center),
              boxShadow: shadows,
              border: border),
        ),
        placeholder: (context, url) => Container(
          height: height,
          width: width,
          margin: margin,
          padding: padding,
          decoration: BoxDecoration(
              color: Color(0xFFF0F0F0),
              borderRadius:
                  radius != null ? radius : BorderRadius.circular(borderRadius),
              boxShadow: shadows,
              border: border),
          child: SpinKitFadingCircle(
            color: EsColors.White,
            size: (height == double.infinity ? 50 : height) * 0.5,
          ),
        ),
        errorWidget: (context, url, error) => _buildEmpty(),
      );
    }
  }

  Container _buildEmpty() {
    return Container(
      height: height,
      width: width,
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        color: Color(0xFFF0F0F0),
        borderRadius:
            radius != null ? radius : BorderRadius.circular(borderRadius),
        boxShadow: shadows,
        image: DecorationImage(
            image: AssetImage('assets/images/nodata.png'),
            fit: BoxFit.scaleDown),
      ),
    );
  }
}
